package com.xujl.app;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rv = findViewById(R.id.rv);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        rv.setLayoutManager(layoutManager);
        List<Bean> list = new ArrayList<>();
        for (int i = 1; i < 7; i++) {
            list.add(new Bean("这是第"+i+"项",getResources().getIdentifier("image" + i, "drawable", BuildConfig.APPLICATION_ID)));
        }
        rv.setAdapter(new Adapter(list));
    }

    private class Adapter extends BaseQuickAdapter<Bean, BaseViewHolder> {
        public Adapter (@Nullable List<Bean> data) {
            super(R.layout.item_test, data);
        }

        @Override
        protected void convert (BaseViewHolder helper, Bean item) {
            helper.setText(R.id.tv, item.getTitle())
                    .setImageResource(R.id.iv, item.getImgSrc());
        }
    }

    private class Bean {
        private String title;
        private int imgSrc;

        public Bean (String title, int imgSrc) {
            this.title = title;
            this.imgSrc = imgSrc;
        }

        public String getTitle () {
            return title;
        }

        public void setTitle (String title) {
            this.title = title;
        }

        public int getImgSrc () {
            return imgSrc;
        }

        public void setImgSrc (int imgSrc) {
            this.imgSrc = imgSrc;
        }
    }
}
